package cn.com.io.day20151214.control;

import cn.com.io.day20151214.pojo.Person;
import cn.com.io.day20151214.pojo.Teacher;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 * 对象引用序列化(一)
 */
public class WriteTeacher {
    public static void main(String[] args) throws IOException {
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("teacher.txt"));//创建一个ObjectOutputStream输出流
            Person person = new Person("张三",23);
            Teacher teacher1 = new Teacher("李四",person);
            Teacher teacher2 = new Teacher("王五",person);
            //依次写入对象
            oos.writeObject(teacher1);
            oos.writeObject(teacher2);
            oos.writeObject(person);
            oos.writeObject(teacher2);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (oos != null){
                oos.close();
            }
        }
    }
}
